package com.mxpio.crm.receipt.controller;

import com.mxpio.crm.receipt.entity.CrmReceipt;
import com.mxpio.crm.receipt.service.ICrmReceiptService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

/**
 * 收款单Controller
 *
 * @author Cheng
 * @date 2022-11-24
 */
@Slf4j
@Tag(name = "收款单Controller", description = "收款单")
@RestController()
@RequestMapping("/receipt")
public class CrmReceiptController {

    @Autowired
    private ICrmReceiptService crmReceiptService;

    /**
     * 查询收款单列表
     */
    @GetMapping("/list")
    @Operation(summary = "收款单", description = "分页查询", method = "GET")
    public Result<Page<CrmReceipt>> list(Criteria criteria,
                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                         @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo) throws Exception {
        Pageable pageAble = PageRequest.of(pageNo - 1, pageSize);
        Page<CrmReceipt> page = crmReceiptService.page(criteria, pageAble);
        return Result.OK(page);
    }

    /**
     * 添加
     */
    @PostMapping(value = "/add")
    @Operation(summary = "收款单-添加", description = "收款单-添加", method = "POST")
    public Result<?> add(@RequestBody CrmReceipt crmReceipt) {
        crmReceiptService.save(crmReceipt);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param crmReceipt
     * @return
     */
    @Operation(summary = "收款单-编辑", description = "收款单-编辑", method = "PUT")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody CrmReceipt crmReceipt) {
        crmReceiptService.update(crmReceipt);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    @Operation(summary = "收款单-通过id删除", description = "收款单-通过id删除")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        crmReceiptService.delete(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/deleteBatch")
    @Operation(summary = "收款单-批量删除", description = "收款单-批量删除")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    @Operation(summary = "收款单-通过id查询", description = "收款单-通过id查询")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        CrmReceipt crmReceipt = crmReceiptService.getById(id);
        if (crmReceipt == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(crmReceipt);
    }


}
